//
//  InvitionViewController.m
//  penzer
//
//  Created by caiyc on 2018/10/3.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "InvitionViewController.h"
#import "invation_Cell.h"
#import "DXAlertView.h"
@interface InvitionViewController ()<UITableViewDelegate,UITableViewDataSource,DXAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataSouce;
@property(assign)BOOL acpct;
@property(nonatomic,strong)NSString *handelId;
@end

@implementation InvitionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = 0;
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadLiveLis)];
    // Do any additional setup after loading the view from its nib.
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self loadLiveLis];
}



//获取直播列表
-(void)loadLiveLis
{
    [self.dataSouce removeAllObjects];
    NSDictionary *param = @{@"ac":@"store",
                            @"op":@"activity",
                            @"ctrl":@"live_anchor",
                            @"ta":@"list",
                            @"status":@"0"
                            };
    [self weqPostHandle:param success:^(id response) {
        [self.tableView.mj_header endRefreshing];
        
        if(response){
            //   [self.kCountDownManagers reload];
            NSArray *datas = response[@"data"][@"list"];
            self.reloadBage(datas.count);
            if([response[@"data"]isKindOfClass:[NSDictionary class]]){
            [self.dataSouce addObjectsFromArray:response[@"data"][@"list"]];
            [self.tableView reloadData];
            }
        }else{
             [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
    }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSouce.count;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 290;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cell";
    invation_Cell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if(!cell){
        cell = [[[NSBundle mainBundle]loadNibNamed:@"invation_Cell" owner:self options:nil]lastObject];
    }
    cell.call=^(){
        NSString *telStr = [NSString stringWithFormat:@"tel:%@",self.dataSouce[indexPath.row][@"telephone"]];
        NSURL *url = [NSURL URLWithString:telStr];
        [[UIApplication sharedApplication]openURL:url];
    };
   [cell bindData:self.dataSouce[indexPath.row]];
    //__block weakself = self;
    cell.handelInvation=^(NSInteger status){
        NSString *title  = @"接受";
        self.handelId = [NSString stringWithFormat:@"%@",self.dataSouce[indexPath.row][@"actanchorid"]];
        if(status==0){
            //接受
            self.acpct = 1;
        }else{
            //拒绝
            title = @"拒绝";
            self.acpct = 0;
        }
        DXAlertView *ve = [[DXAlertView alloc]initWithTitle:@"提示" message:[NSString stringWithFormat:@"确认%@邀请",title] cancelBtnTitle:@"取消" otherBtnTitle:@"确认"];
        ve.delegate = self;
        [ve show];
    };
    return cell;
}


-(void)dxAlertView:(DXAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(buttonIndex==1){
        NSDictionary *param = @{@"ac":@"store",
                                @"op":@"activity",
                                @"ctrl":@"live_anchor",
                                @"ta":@"an_invite",
                                @"status":[NSString stringWithFormat:@"%d",self.acpct],@"actanchorid":self.handelId};
        [self weqPostHandle:param success:^(id response) {
            if(response){
                //操作成功重新加载数据
                [self loadLiveLis];
                [[NSNotificationCenter defaultCenter]postNotificationName:@"acpt" object:nil];
            }
        } failure:^(NSError *error) {
            
        }];
    }
}



#pragma mark--getter&&setter
-(NSMutableArray *)dataSouce{
    if(!_dataSouce){
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}


@end
